package com.hillky.compress.impl;

import com.hillky.compress.Compressor;
import com.hillky.exceptions.CompressException;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * Gzip 压缩和解压的具体实现
 */
@Slf4j
public class GzipCompressor implements Compressor {
    @Override
    public byte[] compress(byte[] bytes) {


        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos);) {
            gzipOutputStream.write(bytes);
            gzipOutputStream.finish();
            byte[] result = baos.toByteArray();
            if(log.isDebugEnabled()){
                log.debug("对字节数组进行了压缩，长度由【{}】压缩至【{}】",bytes.length,result.length);
            }
            return result;
        } catch (IOException e) {
            log.error("对字节数组压缩时发生异常");
            throw new CompressException(e);
        }
    }

    @Override
    public byte[] deCompress(byte[] bytes) {

        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream gzipInputStream=new GZIPInputStream(bais);
            byte[] result = gzipInputStream.readAllBytes();
            if(log.isDebugEnabled()){
                log.debug("对字节数组进行了解压缩，长度由【{}】变为【{}】",bytes.length,result.length);
            }
            return result;
        } catch (IOException e) {
            throw new CompressException("解压缩时发生异常");

        }

    }
}
